<template>
  <div class="box">
    <huocms-fc-designer ref="designer" @saveForm="saveForm" />
  </div>
</template>

<script>
import formApi from '@/api/form.js'
import defaultDesign from '../data/defaultDesign.json'
import defaultOption from '../data/defaultOption.json'

export default {
  name: 'Design',
  props: ['ids'],
  data() {
    return {
      id: 0,
      formdata: {},
      defaultRule: '',
      defaultOption: ''
    }
  },
  created() {
    this.id = this.$route.query.id
  },
  mounted() {
    this.getInfo()
    this.defaultRule = JSON.stringify(defaultDesign)
    this.defaultOption = JSON.stringify(defaultOption)
  },
  methods: {
    async getInfo() {
      const res = await formApi.formInfo({ id: this.id })
      if (res.code == 0) {
        this.formdata = res.data

        if (this.formdata.design_content) {
          this.$refs.designer.setRule(this.formdata.design_content)
        } else if (this.formdata.type == 2) {
          // 设置初始问卷标题和描述
          this.$refs.designer.setRule(this.defaultRule)
          this.$refs.designer.setOption(this.defaultOption)
        }
        if (this.formdata.design_option) {
          this.$refs.designer.setOption(this.formdata.design_option)
        }
      } else {
        this.$emit('success')
      }
    },
    async saveForm() {
      const design_content = this.$refs.designer.getRule()
      const HuocmsFcDesignerOptions = this.$refs.designer.getOption() // HuocmsFcDesigner 生成的`options`
      if (design_content.length == 0) {
        this.$message.error('表单内容不能为空！')
        return false
      }

      const res = await formApi.edit({
        id: this.formdata.id,
        name: this.formdata.name,
        design_content: JSON.stringify(design_content),
        design_option: JSON.stringify(HuocmsFcDesignerOptions)
      })

      if (res.code == 0) {
        this.$emit('success')
        this.$message.success(res.msg)
      }
    }
  }
}
</script>

<style scoped>
.box {
  width: 100%;
  height: 80vh;
  background: #fff;
}

._fc-t-header {
  height: 60px;
  margin: 0 20px;
  position: relative;
  display: flex;
  align-items: center;
  cursor: default;
}

._fc-t-logo {
  height: 26px;
}

._fc-t-name {
  display: inline-block;
  color: rgba(0, 0, 0, 0.8);
  font-size: 20px;
  font-weight: 600;
  margin-left: 5px;
}

._fc-t-menu {
  position: absolute;
  right: 0;
}

._fc-t-menu i {
  font-size: 12px;
}

body {
  min-height: 100vh;
  padding: 0;
  margin: 0;
  display: flex !important;
  flex-direction: column !important;
}

#app {
  display: flex;
  flex-direction: column;
  flex: 1;
}

._fc-copyright {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  padding: 0 20px;
  font-size: 16px;
  border-top: 1px solid #ececec;
  background-color: #fff;
  cursor: pointer;
}

._fc-t-dialog .CodeMirror {
  height: 450px;
}

._fc-t-dialog .CodeMirror-line {
  line-height: 16px !important;
  font-size: 13px !important;
}

.CodeMirror-lint-tooltip {
  z-index: 2021 !important;
}

._fc-t-dialog .el-dialog__body {
  padding: 0px 20px;
}

._fc-b-item {
  display: flex;
}
</style>
